package tcp.updateandcopy;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class TcpServer {

	public static void main(String[] args) throws IOException  {
		// TODO Auto-generated method stub
		//open port
		String localPath="server";
		ServerSocket ss=new ServerSocket(8887);
		Socket acceptS=ss.accept();  //open port action
		System.out.println("server connection............");
		OutputStream os=acceptS.getOutputStream();
		InputStream is=acceptS.getInputStream();
		BufferedReader bufR=new BufferedReader(new InputStreamReader(is));
//		os.write(TcpServer.helpDocument().toString().getBytes());
		//Receive command
		//command div 2:commandKind,fPath
		String commandRecever="";
		String fPath="";
		while((commandRecever=bufR.readLine())!=null)
		{
			//commandKind : CP
			if(commandRecever.equals("CP"))
			{
				os.write("GET COMMAND 1\n\r".getBytes());
				//read fPath
				commandRecever=bufR.readLine();
				os.write("GET COMMAND 2\n\r".getBytes());
				fPath=localPath+"\\"+commandRecever;
				System.out.println("client want file: "+fPath);
				//catch server local file and delivery
				InputStream fileInput = null;
//				FileOutputStream fileOutput = new FileOutputStream(fPath);
				try {
					fileInput = new FileInputStream(fPath);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					os.write("not found file\n".getBytes());
					continue;
				}
				os.write("file found\n\r".getBytes());
				byte[] buf=new byte[fileInput.available()];
				fileInput.read(buf);
				os.write(buf);
				os.write("file end\n\r".getBytes());
				fileInput.close();  //release temporary InputStream
			}
			//commandKind : SCP
			else //commandRecever.equals("SCP")
			{
				 commandRecever=bufR.readLine();
				 OutputStream fileOutput = null;
				try {
					fileOutput = new FileOutputStream(commandRecever);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 System.out.println("file creat success");
				 String recever="";
				 while((recever=bufR.readLine())!=null)
				 {
//					 System.out.println(recever);
					 if(recever.equals("file end"))
						 break;
					 System.out.println(recever);
					 fileOutput.write(recever.getBytes());
					 fileOutput.write("\n".getBytes());
				 }
				 fileOutput.close();
				
			}
				
		}
		os.close();
		bufR.close();
		System.out.println();
	}
	
	public static StringBuilder helpDocument()
	{
		StringBuilder sb=new StringBuilder("");
		sb.append("Read this Help Document\n\r");
		sb.append("Input these command you kan get:\n\r");
		sb.append("CP  lPath ipAddress fPath  Copy file from targetPath on ipAddress to localPath\n\r");
		sb.append("SCP lPath ipAddress fPath  Copy file from localPath to targetPath of ipAddress\n\r");
		sb.append("Q   Stop system!\n\r");
		return sb;
	}

}
